package com.kadron.mail;

import java.util.List;

import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.faces.Renderer;

import com.kadron.common.KUtil;
import com.kadron.entity.NewsLetter;
import com.kadron.entity.User;
import com.kadron.session.KUser;

public class KMailService {

	public static void sendOrder() {
		Renderer renderer = Renderer.instance();
	    try {
	    	Contexts.getEventContext().set("person", KUser.instance().getUser());
	        renderer.render("/pages/mail/orderMail.xhtml");
	   } catch (Exception e) {
	       e.printStackTrace();
	   }
	}
	
	public static void sendReg(String password, User user) throws Exception {
		if (user == null || KUtil.isEmpty(password)) {
			throw new NullPointerException();
		}
		Renderer renderer = Renderer.instance();
		try {
			Contexts.getEventContext().set("person", user);
			Contexts.getEventContext().set("k_password", password);
			renderer.render("/pages/mail/registrationMail.xhtml");
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	public static void sendNewsletter(User user, NewsLetter newsLetter) throws Exception {
		if (user == null) {
			throw new NullPointerException();
		}
		if (!user.isNewsletter()) {
			return;
		}
		Renderer renderer = Renderer.instance();
		try {
			Contexts.getEventContext().set("person", user);
			Contexts.getEventContext().set("nl_subject", newsLetter.getName(user.getLang().name()));
			Contexts.getEventContext().set("nl_content", newsLetter.getDesc(user.getLang().name()));
			renderer.render("/pages/mail/newsletterMail.xhtml");
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	public static void sendBirtdayRemainder(List<User> users, User admin) throws Exception {
		if (users == null || users.isEmpty() || admin == null) {
			throw new NullPointerException();
		}
		Renderer renderer = Renderer.instance();
		try {
			Contexts.getEventContext().set("admin", admin);
			Contexts.getEventContext().set("birthdayUsers", users);
			renderer.render("/pages/mail/birthdayRemainderMail.xhtml");
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
}
